package metronome;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
 * State when metronome is playing.
 * @author Suwijak Suppapit 5510546104
 * @version 5/1/2013
 */
public class PlayState extends MetronomeState{


	private AudioInputStream audioBasic;
	private Clip clipBasic;
	private AudioInputStream audioSpecial;
	private Clip clipSpecial;
	String bpmNum;
	int count = 1;
	/**
	 * Initialize PlayState.
	 * @param metronome metronome
	 */
	public PlayState(Metronome metronome){
		super(metronome);
		bpmNum = "";
		try {
			audioBasic = AudioSystem.getAudioInputStream(this.getClass().getResource("/sound/button-16.wav"));
			clipBasic = AudioSystem.getClip();
			clipBasic.open(audioBasic);
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
		try {
			audioSpecial = AudioSystem.getAudioInputStream(this.getClass().getResource(
					"/sound/button-26.wav"));
			clipSpecial = AudioSystem.getClip();
			clipSpecial.open(audioSpecial);
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void performPlay() {
		
		
	}

	@Override
	public void performStop() {
		count = 1;
		metronome.setState(Metronome.stopState);
		
	}

	@Override
	public void beep() {
		
		
		if(count % 4 != 0){
			try {
				//clipBasic.stop();
				clipBasic.setFramePosition(0);
				clipBasic.start();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			count++;
		}
		else {
			try {
				clipSpecial.setFramePosition(0);
				clipSpecial.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
			count++;
		}
		
	}
	

}
